<?php
/**
 * Created by PhpStorm.
 * User: jungshen
 * Date: 2018/8/3
 * Time: 10:54
 */
namespace app\api\controller\other;

use app\api\controller\BasicApi;
use think\Db;

class Supplier extends BasicApi
{
    /**
     * 获取供应商首页品牌推荐数据
     * @return \think\response\Json
     */
    public function recommend()
    {
        //查询全部推荐供应商
        $province=input('province','500000000000');
        $list=Db::name('store_member')->alias('m')->join(['supplier_info'=>'si'],'m.id=si.mid')
            ->where('m.type',USER_TYPE_SUPPLIER)->where('si.expiry_time','>',time())
            //->where('m.province',$province)
            ->where('m.status',1)
            ->whereRaw('find_in_set('.FLAG_RECOMMEND.',m.flag) ')
            ->field('m.id,headimg,si.id si_id,si.name')
            ->limit(30)->cache(60)->order('sort,grade_id desc,id desc')->select();
        $i=0;
        $res[$i]=[];
        foreach ($list as $k=>$v){
            $res[$i][]=$v;
            if(count($res[$i])>5){
                $i++;
            }
        }
        return json(['msg'=>'success','data'=>$res]);
    }

    /**
     * 获取供应商列表
     * @return \think\response\Json
     */
    public function supplier_list()
    {
        try{
            $name=input('name');
            $province=input('province');
            $city=input('city');
            $district=input('district');
            $supplier_type=input('supplier_type');

            $page_size=input('page_size',10);
            $page_now=input('page_now',1);
            $is_recommend=input('is_recommend',1);
            $lat=input('lat','29.34588019989763200');
            $lng=input('lng','106.65887899864005000');
            $db=Db::name('store_member')->alias('m')
                ->join(['supplier_info'=>'si'],'m.id=si.mid')
                ->where('m.type',USER_TYPE_SUPPLIER)
                ->where('m.status',1)
                ->where('si.expiry_time','>',time())//tmp 处理空距离
                ->field('m.id,headimg,province,city,district,address,lng,lat,phone,si.id si_id,si.name,main,grade_id,m.credit,
                if(isnull(lat),1,0) tmp,
                (2 * 6378.137* ASIN(SQRT(POW(SIN(3.1415926535898*('.$lat.'-lat)/360),2)+COS(3.1415926535898*'.$lat.'/180)* COS(lat * 3.1415926535898/180)*POW(SIN(3.1415926535898*('.$lng.'-lng)/360),2))))*1000 as juli');
            if($name){
                //查询所有产品
                $supplier_goods=Db::name('supplier_goods')
                    ->where('name','like','%'.$name.'%')
                    ->where('status',1)
                    ->group('mid')
                    ->column('mid');
                if($supplier_goods){
                    $db->where(function ($query) use ($name,$supplier_goods){
                        $query->where('si.name','like','%'.$name.'%')
                            ->whereOr('si.mid','in',$supplier_goods);
                    });
                }else{
                    $db->whereLike('si.name','%'.$name.'%');
                }
            }
            if($is_recommend==1){
                $db->whereRaw('find_in_set('.FLAG_RECOMMEND.',m.flag) ');
            }
            /*if($province){
                $db->where('province',$province);
            }
            if($city){
                $db->where('city',$city);
            }
            if($district){
                $db->where('district',$district);
            }*/
            if($supplier_type){
                $db->whereRaw('find_in_set('.$supplier_type.',si.supplier_type_ids)');
            }

            $list=$db->page($page_now,$page_size)->order('sort,grade_id desc,tmp,juli,credit desc,id desc')->select();
//            $list=$db->page($page_now,$page_size)->order('tmp,juli,sort,grade_id desc,id desc')->select();
            //$list=$db->page($page_now,$page_size)->orderRaw('if(isnull(juli),1,0),juli,sort,grade_id desc,id desc')->buildSql();
            //$list=$db->page($page_now,$page_size)->order(['if(isnull(juli),1,0)','juli','sort','grade_id'=>'desc'])->buildSql();
            foreach ($list as $k=>&$v){
                $v['full_address']=address_tostring($v,0,' ');
                $v['main_arr']=Db::name('my_cate')->where('id','in',$v['main'])->field('title')->select();
                //查询这个人是否实名认证了
                $v['authentication_type_text']=get_authentication_text($v['id']);
                $v['star_num']=$v['grade_id']==4?3:1;
                $v['state_of_payment']=(int)Db::name(ORDER_TYPE_EARNEST_ORDER_TABLE)->where('mid',$v['id'])->where('status',1)->count();
            }

            return json(['msg'=>'success','data'=>$list]);
        }catch(\Exception $e){
            return json(['msg'=>$e->getMessage()],500);
        }
    }
    /**
     * 获取商家详细页面信息
     * @return \think\response\Json
     */
    public function info()
    {
        try{
            $mid=input('mid');
            Db::name('store_member')->where('id',$mid)->setInc('browse');
            //查询详细信息
            $data['info']=Db::name('store_member')->alias('m')->join(['supplier_info'=>'si'],'m.id=si.mid')
                ->field('m.id,province,city,district,address,phone,si.id si_id,si.name,main,tel,intro,supplier_type_ids,lng,lat,si.images,grade_id,browse,credit')
                ->where('m.id',$mid)->where('m.type',USER_TYPE_SUPPLIER)->where('si.expiry_time','>',time())
                ->find();
            $data['info']['authentication_type_text']=get_authentication_text($data['info']['id']);
            $data['info']['full_address']=address_tostring($data['info']);
            $data['info']['images']=explode('|',$data['info']['images']);
            $data['info']['supplier_type_arr']=Db::name('my_cate')->where('id','in',$data['info']['supplier_type_ids'])
                ->field('title')->select();

            $data['info']['star_num']=$data['info']['grade_id']==4?3:1;
            $data['info']['state_of_payment']=(int)Db::name(ORDER_TYPE_EARNEST_ORDER_TABLE)->where('mid',$data['info']['id'])->where('status',1)->count();
            //查询5个案例
            $case=Db::name('member_case')->where('mid',$mid)->where('status',1)->limit(5)->select();
            foreach ($case as $k=>$v){
                $case[$k]['images']=explode('|',$v['images']);
                $case[$k]['addtime']=date('Y-m-d',$v['addtime']);
                //获取赞数量
                $case[$k]['praised']=Db::name('praise')->where('attach_id',$v['id'])->where('type',1)->count();
            }
            $data['case']=$case;

            //查询前5个产品
            $pros=Db::name('supplier_goods')->where('mid',$mid)->where('status',1)->field('image')->limit(5)->select();
            $data['product']=$pros;
            //查询最新5个动态
            $dynamics=Db::name('dynamic')->where('mid',$mid)->where('status',1)->order('id desc')/*->limit(5)*/
                ->field('id,content,images,FROM_UNIXTIME(create_at,\'%Y-%m-%d\') addtime')->cache(60)->select();
            foreach ($dynamics as $k=>$v){
                $dynamics[$k]['images']=explode('|',$v['images']);
                $dynamics[$k]['praised']=Db::name('praise')->where('attach_id',$v['id'])->where('type',2)->count();
            }
            $data['dynamic']=$dynamics;
            return json(['msg'=>'success','data'=>$data]);
        }catch(\Exception $e){
            return json(['msg'=>$e->getMessage()],500);
        }
    }

    /**
     * 获取供应商产品信息
     * @return \think\response\Json
     */
    public function supplier_goods_list()
    {
        try{
            $mid=input('mid');
            $list=Db::name('supplier_goods')->where('mid',$mid)->where('status',1)->cache(60)->order('sort')->select();
            return json(['msg'=>'success','data'=>$list]);
        }catch(\Exception $e){
            return json(['msg'=>$e->getMessage()],500);
        }
    }
}